package com.hpeng.basic.action;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.hpeng.basic.service.OtherDataService;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("otherDataAction")
@Scope("prototype")
public class OtherDataAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private OtherDataService otherDataService ;
	@Resource
	private Function function;
	private AssetCard ac;
	private  Pagination<AssetCard> pagination;
	private int page;
	private String pageLink;
	private String catid;
	private String acid;
	private String idArray; // 获取被选择的Id编号数组 从页面上传到后台的数组
	private String msg;
	private String accessRight;
	private String FN;
	private List<Lookup> ls;// 用于页面上显示资产的状态的list
	/**
	 * 页面跳转
	 * @return
	 */
	public String toPage(){
		return "topage";
	}
	/**
	 * 添加或修改
	 * @return
	 */
	public String saveOrUpdateAc(){
		boolean var =false;
		if((ac.getAcid()) != null && !(ac.getAcid()).equals("")){
			var = true;
		}
		if(var){
			otherDataService.UpdateAssetCard(ac);
		}else{
			otherDataService.saveAc(ac);
		}
		return "saveOrUpdate";
	}
	
	/**
	 * 分页显示数据
	 * @return
	 */
	public String getAssetCard(){
		String[] hql = {"from AssetCard a"};
		String values="";
		pagination =otherDataService.getAssetCard(page, hql, values);
		pageLink = pagination.getPageLink("ac/getAssetCard.action");
		this.accessRight = function.getAccessRight(session(), FN);
		return "getAssetCard";
	}
	
	/**
	 * 根据ID获取数据
	 */
	public String getAssetCardById(){
		ac = otherDataService.getAssetCardById(acid);
		return "getAssetCardById";
	}
	
	/**
	 * 删除数据
	 * @return
	 */
	public String delAssetCard(){
		try {
			String[] stringArray = idArray.split(",");
			System.out.println(stringArray.length);
			for (int i = 0; i < stringArray.length; i++) {
				AssetCard ass = otherDataService.getAssetCardById(stringArray[i]);
				if(ass.getAcstatus().equals("110")){
					otherDataService.delAssetCard(stringArray[i]);
				}else{
					msg = getText("只能删除申报-经办已录入的资产");
				}
			}
		} catch (RuntimeException rx) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		
		return "delAssetCard";
	}
	
	/**
	 * 跳转到添加或修改页面
	 * @return
	 */
	public String toUpdateOrAdd(){
		return "toUpdate";
	}
	
	/**
	 * 根据产品类别
	 * @return
	 */
	public String byCatId(){
		String[] hql = {"from AssetCard a where a.category.catid = "+catid};
		String order = " ";
		setPagination(otherDataService.getAssetCardByCatId(page, hql, order));
		pageLink = pagination.getPageLink("ac/bycatid.action?FN=" + FN+"&catid="+catid);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "bycatid";
	}
	
	public Map<String, Object> session() {
		return ActionContext.getContext().getSession();
	}
	
	
	/******************************************************************************************/
	public List<Lookup> getLs() {
		return ls;
	}
	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}
	public String getFN() {
		return FN;
	}
	public void setFN(String fN) {
		FN = fN;
	}
	public String getAccessRight() {
		return accessRight;
	}
	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getIdArray() {
		return idArray;
	}
	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}
	public String getAcid() {
		return acid;
	}
	public void setAcid(String acid) {
		this.acid = acid;
	}
	public AssetCard getAc() {
		return ac;
	}
	public void setAc(AssetCard ac) {
		this.ac = ac;
	}
	public Pagination<AssetCard> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getPageLink() {
		return pageLink;
	}
	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}
	public String getCatid() {
		return catid;
	}
	public void setCatid(String catid) {
		this.catid = catid;
	}
}
